package br.com.hotel68.view.gui;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import javax.persistence.PersistenceException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;

import br.com.hotel68.Hotel68BusinessFacade;
import br.com.hotel68.model.Client;
import br.com.hotel68.model.Room;
import br.com.hotel68.view.gui.model.ClientTableModel;
import br.com.hotel68.view.gui.model.RoomTableModel;

public class MainWindow {

	protected JFrame frame;
	protected JTable roomsTable;
	protected JTabbedPane tabbedPane;
	private Hotel68BusinessFacade facade;

	/**
	 * Create the application.
	 */
	public MainWindow() {
		

		//INFORMACOES DUMMY ENQUANTO NAO TEMOS ISTO FUNCIONAL
		String[] colNames = {"Apartamento","Situacao","Tipo","Hospede","Check-in"};
		String[][] data = {	{"101","OCUPADO","Suite","Joao","21/05/2012"},
							{"102","RESERVADO","Luxo","Jose",""},
							{"103","LIVRE","","",""},
							{"104","LIVRE","","",""},
							{"105","GOVERNANCA","","",""},
							{"106","OCUPADO","Luxo","Joaquim","10/07/2012"},
							{"107","OCUPADO","Suite","Jose 2","12/09/2012"}
		};
		roomsTable = new JTable(data,colNames);
		
		//INICIALIZA A INTERFACE
		initialize();
	}
	
	public void showInterface(){
		
		this.frame.setVisible(true);
		
		//TESTA ACESSO AO BANCO ANTES DE COMECAR O PROGRAMA
		String configFile = "hotel68.properties";
		Properties properties = new Properties();
		try {
		    properties.load(new FileInputStream(configFile));
			facade = new Hotel68BusinessFacade(properties);
		    //DEI UM SLEEP DE 3 SEGUNDOS PRA DAR UM SUSPENSE! :)
			try { Thread.currentThread().sleep(3000); }
			catch ( Exception e ) { System.out.println("error");}
			
		} catch (PersistenceException e) {
			System.out.println("Erro ao carregar o banco" + configFile + "'");
			JOptionPane.showMessageDialog(frame,"Erro ao carregar o banco de dados" + configFile + "'","ATENÇÃO",JOptionPane.ERROR_MESSAGE);
			return;	
		} catch (IOException e1) {
			System.out.println("Erro ao carregar os parametros do sistema do arquivo '" + configFile + "'");
			System.out.println("\tMensagem: " + e1.getMessage());
			JOptionPane.showMessageDialog(frame,"Erro ao carregar os parametros do sistema do arquivo '" + configFile + "'","ATENÇÃO",JOptionPane.ERROR_MESSAGE);
			return;	
		}
		
		
		//SE PASSAR CRIA O FACADE E MOSTRA A MAIN SCREEN

		this.tabbedPane.setVisible(true);
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("HOTEL 68");
		frame.setIconImage(new ImageIcon("img/logop.png").getImage());
		frame.getContentPane().setBackground(SystemColor.activeCaption);
		
		JPanel panel_2 = new JPanel();
		frame.getContentPane().add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(null);
		//lblNewLabel.removeAll();
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 5, 564, 546);
		panel_2.add(tabbedPane);
		tabbedPane.setVisible(false);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("Recep\u00E7\u00E3o", null, panel, null);
		panel.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 539, 428);
		panel.add(scrollPane);
		
		//roomsTable = new JTable();
		roomsTable.setEnabled(false);
		scrollPane.setViewportView(roomsTable);
		
		JButton btnCheckin = new JButton("CHECK-IN");
		btnCheckin.setBounds(10, 450, 137, 23);
		panel.add(btnCheckin);
		
		JButton btnCheckout = new JButton("CHECK-OUT");
		btnCheckout.setBounds(10, 484, 137, 23);
		panel.add(btnCheckout);
		
		JButton btnExtratoDeConta = new JButton("EXTRATO DE CONTA");
		btnExtratoDeConta.setBounds(365, 484, 184, 23);
		panel.add(btnExtratoDeConta);
		
		JButton btnImprimirLista = new JButton("IMPRIMIR LISTA");
		btnImprimirLista.setBounds(365, 450, 184, 23);
		panel.add(btnImprimirLista);
		
		JButton btnReservar = new JButton("RESERVAR");
		btnReservar.setBounds(157, 450, 137, 23);
		panel.add(btnReservar);
		
		JButton btnBloquear = new JButton("CONSUMO");
		btnBloquear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//new AddAmount(facade.findHostingById(1), facade);
			}
		});
		btnBloquear.setBounds(157, 484, 137, 23);
		panel.add(btnBloquear);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(new ImageIcon("img/logop.png"));
		lblNewLabel_1.setBounds(304, 456, 51, 51);
		panel.add(lblNewLabel_1);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Admin", null, panel_1, null);
		panel_1.setLayout(null);
		
		JButton button = new JButton("Adicionar Quarto");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		button.setFont(new Font("Tahoma", Font.BOLD, 15));
		button.setBounds(143, 322, 272, 46);
		panel_1.add(button);
		
		JButton button_1 = new JButton("Adicionar Cliente");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new ClientWindow().setVisible(true);
			}
		});
		button_1.setFont(new Font("Tahoma", Font.BOLD, 15));
		button_1.setBounds(143, 81, 272, 46);
		panel_1.add(button_1);
		
		JButton button_2 = new JButton("Listar Quartos");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Collection<Room> rooms = facade.findRooms();
				RoomTableModel tableModel = new RoomTableModel(rooms);
				new ReportsWindow( tableModel );
			}
		});
		button_2.setFont(new Font("Tahoma", Font.BOLD, 15));
		button_2.setBounds(143, 379, 272, 46);
		panel_1.add(button_2);
		
		JButton button_3 = new JButton("Adicionar Tipo de Quarto");
		button_3.setFont(new Font("Tahoma", Font.BOLD, 15));
		button_3.setBounds(143, 436, 272, 46);
		panel_1.add(button_3);
		
		JButton btnListarClientes = new JButton("Listar Clientes");
		btnListarClientes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Collection<Client> clients = facade.findClients();
				ClientTableModel tableModel = new ClientTableModel(clients);
				new ReportsWindow( tableModel );
			}
		});
		btnListarClientes.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnListarClientes.setBounds(143, 138, 272, 46);
		panel_1.add(btnListarClientes);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 252, 539, 13);
		panel_1.add(separator);
		
		JLabel lblNewLabel_2 = new JLabel("QUARTOS");
		lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_2.setBounds(193, 265, 173, 31);
		panel_1.add(lblNewLabel_2);
		
		JLabel lblClientes = new JLabel("CLIENTES");
		lblClientes.setHorizontalAlignment(SwingConstants.CENTER);
		lblClientes.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblClientes.setBounds(193, 11, 173, 31);
		panel_1.add(lblClientes);
		
		JLabel lblNewLabel = new JLabel("LOADING...");
		lblNewLabel.setBounds(0, 0, 584, 562);
		panel_2.add(lblNewLabel);
		lblNewLabel.setIcon(new ImageIcon("img/logo68.png"));
		lblNewLabel.setForeground(new Color(255, 255, 255));
		lblNewLabel.setBackground(new Color(25, 25, 112));
		lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel.setOpaque(true);
		frame.setBounds(100, 100, 600, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
